<template>
  <div>
  <el-card>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-row  style="margin-top: 10px">
      <el-button type="primary" @click="openAddNewDialog()"  style="margin-left: 10px">新增</el-button>
      <download-excel
          style="float: left"
          class="export-excel-wrapper"
          :data="authorityData"
          type="xls"
          worksheet="worksheet"
          name="权限信息">
        <el-button type="primary">导出</el-button>
      </download-excel>
    </el-row>
    <el-table
        :data="authorityData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="权限编号"
          width="100">
      </el-table-column>
      <el-table-column
          prop="name"
          label="权限名称">
      </el-table-column>
      <el-table-column
          prop="roleName"
          label="角色名称">
      </el-table-column>
      <el-table-column label="操作"  width="200px"
                       align="center">
        <template slot-scope="scope">
<!--          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>-->
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
<!--    &lt;!&ndash; 修改数据的表单 &ndash;&gt;
    <el-dialog title="编辑用户管理数据" :visible.sync="editFormVisible">
      <el-form :model="editForm"  label-width="120px">
        <el-form-item label="权限编号" prop="roleId">
          <el-input v-model="editForm.id"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="权限内容" prop="value">
          <el-input v-model="editForm.value"></el-input>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>-->
    <!-- 新增数据的表单 -->
    <el-dialog title="新增权限数据" :visible.sync="FormVisible" width="30%">
      <el-select v-model="addInfo.role" placeholder="角色" >
        <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="addInfo.authority" placeholder="权限">
        <el-option
            v-for="item in authList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button @click="FormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAuthority()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Authority",
  data: function () {
    return {
      authority:{},
      editFormVisible: false,
      FormVisible: false,
      authorityData: [],
      roleList:[],
      authList:[],
      addInfo:{role:'',authority:''},
      editForm: {
        id: '',
        name: '',
        roleName: '',
        value:''
      },
      addNewFormVisible: false,
    }
  },
  methods: {
    // 打开新增对话框
    openAddNewDialog(tableItem) {
      this.FormVisible = true
    },
    // 执行新增
    addAuthority() {
      let url = 'http://localhost:8080/v1/admins/'+this.addInfo.role+'/'+this.addInfo.authority+'/new-add';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '添加成功！',
            type: 'success'
          });
          this.FormVisible = false;
          this.loadRoleList();
        }
      });
    },
    /*// 弹出修改对话框
    openEditDialog(tableItem) {
      let url = 'http://localhost:8080/v1/admins/' + tableItem.id + "/authority"
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log('jsonResult =' + jsonResult)
        if (jsonResult.code == 1) {
          this.editForm = jsonResult.data;
          this.editFormVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
     // 执行修改
    handleEdit() {
      let url = 'http://localhost:8080/v1/admins/' + this.editForm.id + '/updateAuthority';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code == 1) {
          this.$message({
            message: '修改标签成功！',
            type: 'success'
          });
          this.editFormVisible = false;
          this.loadRoleList();
        } else if (jsonResult.state == 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.editFormVisible = false;
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },*/
    // 打开删除确认框
    openDeleteConfirm(authority) {
      let message = '此操作将永久删除【' + authority.name + '】角色，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(authority);
      }).catch(() => {
      });
    },
   // 处理删除
    handleDelete(authority) {
      let url = 'http://localhost:8080/v1/admins/' + authority.id + '/deleteAuthority';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code == 1) {
          this.$message({
            message: '删除【' + authority.name + '】成功！',
            type: 'success'
          });
          this.loadRoleList();
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    authorityHandleEdit(index, user) {
      console.log(index);
    },
    authorityHandleDelete(index) {
      console.log(index);
    },
    loadRoleList() {
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get("http://localhost:8080/v1/admins/roleAuthorityList").then((response) => {
        if (response.data.code == 1) {
          this.authorityData = response.data.data;
          console.log(this.authorityData)
        }
      })
    },
  },
  mounted() {
    this.loadRoleList();
    this.axios
        .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
        .get("http://localhost:8080/v1/admins/roleList").then((response) => {
      if (response.data.code == 1) {
        this.roleList = response.data.data;
        console.log(this.roleList)
      }
    })
    this.axios
        .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
        .get("http://localhost:8080/v1/admins/authorityList").then((response) => {
      if (response.data.code == 1) {
        this.authList = response.data.data;
        console.log(this.authList)
      }
    })
  },
}
</script>

<style scoped>

</style>